import request from '@/utils/request'

const URL = '/apartment/pre/register'

// 获取预登记列表
export function list(params) {
  return request({
    url: URL + '/list',
    method: 'get',
    params: params
  })
}

// 新增预登记信息
export function save(data) {
  return request({
    url: URL,
    method: 'post',
    data: data
  })
}

// 编辑预登记信息
export function update(data) {
  return request({
    url: URL,
    method: 'put',
    data: data
  })
}

// 获取预登记详情
export function getInfo(preRegisterId) {
  return request({
    url: URL + `/${preRegisterId}`,
    method: 'get'
  })
}

// 审核预登记
export function audit(preRegisterId, status, remark) {
  return request({
    url: URL + `/audit/${preRegisterId}`,
    method: 'put',
    params: {
      auditStatus: status,
      auditComment: remark
    }
  })
}

// 分配房间
export function assignRoom(preRegisterId, roomId, rentInfo = null) {
  const data = {
    roomId
  };
  
  // 如果有租金租期信息，添加到请求数据中
  if (rentInfo) {
    data.checkInStart = rentInfo.checkInStart;
    data.checkInEnd = rentInfo.checkInEnd;
    data.rentCollectType = rentInfo.rentCollectType;
    data.rent = rentInfo.rent;
    data.deposit = rentInfo.deposit;
    data.rentType = rentInfo.rentType;
    data.remark = rentInfo.remark;
  }
  
  return request({
    url: URL + `/assign/${preRegisterId}`,
    method: 'put',
    data: data
  })
}

// 办理入住
export function checkIn(preRegisterId, roomId, formData = null) {
  const data = {
    roomId,
    registerId: preRegisterId
  };
  
  // 如果有租金租期信息，添加到请求数据中
  if (formData) {
    data.checkInStart = formData.checkInStart;
    data.checkInEnd = formData.checkInEnd;
    if (formData.rentObject) {
      data.rentObject = formData.rentObject;
    }
    data.remark = formData.remark;
  }
  
  return request({
    url: URL + `/check-in`,
    method: 'put',
    data: data
  })
}

// 删除预登记
export function remove(preRegisterIds) {
  return request({
    url: URL + `/${preRegisterIds}`,
    method: 'delete'
  })
}

export default {
  list,
  save,
  update,
  getInfo,
  audit,
  assignRoom,
  checkIn,
  remove
} 